import JsEncrypt from 'jsencrypt_only_encrypt'
import { key } from "@/api/login-base";

/**
 * RSA 加密，每次从api请求公钥
 * @param password 加密内容，可以是String，Array，Array形式以相同顺序返回结果数组
 * @returns {Promise<any>}
 */
export function passEncrypt(password) {
  return new Promise((resolve, reject) => {
    key().then(res => {
      const encrypt = new JsEncrypt();
      encrypt.setPublicKey(res.data);
      if (password instanceof Array) {
        let ret = password.map(item => encrypt.encrypt(item));
        resolve(ret)
      } else {
        resolve(encrypt.encrypt(password));
      }
    }).catch(reject)
  })
}


export function passDecrypt(password) {
  return new Promise((resolve, reject) => {
    key().then(res => {
      const encrypt = new JsEncrypt();
      encrypt.setPublicKey(res.data);
      if (password instanceof Array) {
        let ret = password.map(item => encrypt.encrypt(item));
        resolve(ret)
      } else {
        resolve(encrypt.encrypt(password));
      }
    }).catch(reject)
  })
}

